package Data_IO.data_json;

import Data_Structure.DataType.MyFlight;
import Data_Structure.DataType.Traveller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Data
/**
 * The type Json Traveller.
 *
 * @auther SHunhao Yan
 * @version 2.0
 */
public class Json_Traveller{

    /**
     * The JSONArray to store the data.
     */
    JSONArray jsonarray = new JSONArray();

    /**
     * Add json.
     *
     * @param object the object
     */
    //Add a jsonObject
    public void addJson(Object object){
        JSONObject JsonOb = (JSONObject) JSON.toJSON(object);
        JSON json = (JSON) JSON.toJSON(object);
        jsonarray.add(JsonOb);

    }

    /**
     * Add json attribute.
     *
     * @param fullname  the fullname
     * @param attribute the attribute
     * @param string    the string
     */
    //Create and add an attribute
    public void addJson_attribute(String fullname, String attribute, String string){
        boolean temp = false;
        for(int i = 0; i < jsonarray.size(); i++){
            if(jsonarray.getJSONObject(i).containsValue(fullname)){
                System.out.println("Exist!");
                temp = true;
                jsonarray.getJSONObject(i).put(attribute,string);
                System.out.println("Successfully operated");
                break;
            }
        }
        if(temp == false){ System.out.println("Invalid input");}
    }


    /**
     * Return the idx of desired traveller.
     * @param bookingNumber
     * @return The idx of desired traveller
     */
    public int returnIdx(String bookingNumber){
        for(int i = 0; i < jsonarray.size(); i++){
            for(int j = 0; j < jsonarray.getJSONObject(i).getJSONObject("flight").size(); j++){
                String str = "flight" + (j+1);
                if(jsonarray.getJSONObject(i).getJSONObject("flight").getJSONObject(str).getString("myBookingNumber").equals(bookingNumber)){

                    return i;
                }
            }
        }
        return 0;
    }

    /**
     * Return the idx of desired myFlight.
     * @param bookingNumber
     * @return The String idx of desired myFlight
     */
    public String returnMyFlightInx(String bookingNumber){
        for(int i = 0; i < jsonarray.size(); i++){
            for(int j = 0; j < jsonarray.getJSONObject(i).getJSONObject("flight").size(); j++){
                String str = "flight" + (j+1);
                if(jsonarray.getJSONObject(i).getJSONObject("flight").getJSONObject(str).getString("myBookingNumber").equals(bookingNumber)){

                    return str;
                }
            }
        }
        return null;
    }

    /**
     * Delete json.
     *
     * @param fullname the fullname
     */
    public void deleteJson(String fullname){
        boolean temp = false;
        for(int i = 0; i < jsonarray.size(); i++){
            if(jsonarray.getJSONObject(i).containsValue(fullname)){
                System.out.println("Exist!");
                temp = true;
                jsonarray.remove(i);
                System.out.println("Successfully operated");
                break;
            }
        }
        if(temp == false){ System.out.println("Invalid input");}
    }


    /**
     * Search the requested traveller with bookingNumber and return the corresponding JsonObject.
     * @param bookingNumber
     * @return The requested traveller in JsonObject
     */
    public JSONObject searchBookingNumber_returnTraveller(String bookingNumber){

        for(int i = 0; i < jsonarray.size(); i++){
            for(int j = 0; j < jsonarray.getJSONObject(i).getJSONObject("flight").size(); j++){
                String str = "flight" + (j+1);
                if(jsonarray.getJSONObject(i).getJSONObject("flight").getJSONObject(str).getString("myBookingNumber").equals(bookingNumber)){
                    System.out.println("Traveller Exist!");
                    return jsonarray.getJSONObject(i);
                }
            }
        }
        return null;
    }

    /**
     * Search the requested traveller with bookingNumber and return the corresponding JsonObject.
     * @param flightID
     * @return The requested traveller in JsonObject
     */
    public JSONArray searchFlightID_returnTraveller(String flightID){

        JSONArray jsonArrayForStaff = new JSONArray();

        for(int i = 0; i < jsonarray.size(); i++){
            for(int j = 0; j < jsonarray.getJSONObject(i).getJSONObject("flight").size(); j++){
                String str = "flight" + (j+1);
                if(jsonarray.getJSONObject(i).getJSONObject("flight").getJSONObject(str).getString("myFlightID").equals(flightID)){
                    System.out.println("Traveller Exist!");
                    jsonArrayForStaff.add(jsonarray.getJSONObject(i));
                }
            }
        }
        return jsonArrayForStaff;
    }

    /**
     * Search the requested myFlight which is owned by traveller with bookingNumber and return the corresponding JsonObject.
     * @param bookingNumber
     * @return The requested myFlight in JsonObject
     */
    public JSONObject searchBookingNumber_returnMyFlight(String bookingNumber){

        for(int i = 0; i < jsonarray.size(); i++){
            for(int j = 0; j < jsonarray.getJSONObject(i).getJSONObject("flight").size(); j++){
                String str = "flight" + (j+1);
                if(jsonarray.getJSONObject(i).getJSONObject("flight").getJSONObject(str).getString("myBookingNumber").equals(bookingNumber)){
                    System.out.println("MyFlight Exist!");
                    return jsonarray.getJSONObject(i).getJSONObject("flight").getJSONObject(str);
                }
            }
        }
        return null;
    }

    /**
     * Search the requested Traveller with personID and return the corresponding JsonObject.
     * @param personID
     * @return The requested Traveller in JsonObject
     */
    public JSONObject searchPersonID_returnTraveller(String personID){

        for(int i = 0; i < jsonarray.size(); i++){
            if(jsonarray.getJSONObject(i).getString("personID").equals(personID)){
                return jsonarray.getJSONObject(i);
            }
        }
        return null;
    }

    /**
     * Sync the JsonArray extracted from the json file.
     * @param ja
     */
    public void syncJsonArray(JSONArray ja){
        jsonarray.clear();
        jsonarray.addAll(ja); //upload the .js file content into jsonarray
    }
}
